/*
 * JANN - a Java toolkit for creating arbitrary Artificial Neural Networks.
 * 
 * Copyright (c) 2009 Matthijs Snel
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package net.jann.ui;

import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.util.List;

import net.jann.Layer;
import net.jann.Neuron;

import org.jhotdraw.draw.Figure;
import org.jhotdraw.draw.GroupFigure;

public class LayerFigure extends GroupFigure {

	private static final long serialVersionUID = -7075911872587032069L;
	
	protected Layer model;
	
	public LayerFigure() {
		super();
		setModel( new Layer() );
	}

	public LayerFigure( double x, double y ) {
		this();
		setStartPoint(x, y);
	}
	
	public LayerFigure( Layer model ) {
		super();
		setModel( model );
	}
	
	public Layer getModel() {
		return model;
	}

	public void setModel(Layer model) {
		if ( model != null )
			removeAllChildren();
		this.model = model;
		initFromModel();
	}
	
	public void setStartPoint( double x, double y ) {
		Rectangle2D.Double rect = getBounds();
		setBounds( new Point2D.Double(x,y), new Point2D.Double(x + rect.width, y + rect.height));
	}
	
	protected void initFromModel() {
		List<Neuron> neurons = model.getNeurons();
		NeuronFigure fig;
		double currx = getStartPoint().x,
				y = getStartPoint().y;
		for( Neuron n : neurons ) {
			fig = new NeuronFigure( n );
			fig.setStartPoint( currx, y );
			add(fig);
			currx += 2 * NeuronFigure.DEFAULT_WIDTH;
		}
	}
}
